// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Diagnostics;

namespace SoapTrace.Core.Formatters
{
	/// <summary>
	/// An object that formats soap message parameters.  Used by the 
	/// <see cref="SoapMessageFormatter"/> class.
	/// </summary>
	public class ParameterValueFormatter
	{
		/// <summary>
		/// The web service parameters that will be used to format a parameter value.
		/// </summary>
		WebServiceParameterCollection m_Parameters;

		/// <summary>
		/// Creates a new instance of the <see cref="ParameterValueFormatter"/>
		/// class.
		/// </summary>
		public ParameterValueFormatter( )
		{
		}

		/// <summary>
		/// Creates a new instance of the <see cref="ParameterValueFormatter"/>
		/// class.
		/// </summary>
		/// <param name="parameters">The parameters that will be reference
		/// when formatting a parameter.</param>
		public ParameterValueFormatter( WebServiceParameterCollection parameters )
		{
			m_Parameters = parameters;
		}

		/// <summary>
		/// Formats a parameter name and value pair.
		/// </summary>
		/// <param name="index">The parameter index.</param>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="valueText">The parameter's value.</param>
		/// <returns>A formatted parameter name-value pair string.</returns>
		public string Format( int index, string name, string valueText )
		{
			bool DisplayParameter = true;
			
			// Find the corresponding parameter.

			WebServiceParameter Parameter = null;
			
			if( m_Parameters != null )
			{
				Parameter = GetParameter( m_Parameters, index );
			}
			
			// Get the formatted value.

			string FormattedValue = valueText;

			if( Parameter != null )
			{					
				DisplayParameter = Parameter.Display;
				
				if( DisplayParameter )
				{
					FormattedValue = ValueFormatter.Format( 
					    Parameter.ParameterType,
						valueText );
				}
			}

			// Format the name-value pair.

			string FormattedText = String.Empty;
			
			if( DisplayParameter )
			{
				FormattedText = FormatNameValuePair( name, FormattedValue );
			}
			
			return FormattedText;
		}
		/// <summary>
		/// Formats a parameter name and value pair.
		/// </summary>
		/// <param name="index">The parameter index.</param>
		/// <param name="arrayIndex">The array index.</param>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="valueText">The parameter's value.</param>
		/// <returns>A formatted parameter name-value pair string.</returns>
		public string Format( int index, int arrayIndex, string name, string valueText )
		{
			bool DisplayParameter = true;
			
			// Find the corresponding parameter.

			WebServiceParameter Parameter = null;
			
			if( m_Parameters != null )
			{
				Parameter = GetParameter( m_Parameters, index );
			}
			
			// Get the formatted value.

			string FormattedValue = valueText;

			if( Parameter != null )
			{					
				if( Parameter.IsArray )
				{
					if( Parameter.Items.Count > arrayIndex )
					{
						DisplayParameter = Parameter.Items[ arrayIndex ].Display;
						
						if( DisplayParameter )
						{
							FormattedValue = ValueFormatter.Format( Parameter.Items[ arrayIndex ].ItemType,
							                                       valueText );
						}
					}
					else
					{
						DisplayParameter = Parameter.Display;
					
						if( DisplayParameter )
						{
							FormattedValue = ValueFormatter.Format( 
							    Parameter.ArrayType,
								valueText );
						}						
					}
				}
				else
				{
					DisplayParameter = Parameter.Display;
				
					if( DisplayParameter )
					{
						FormattedValue = ValueFormatter.Format( 
						    Parameter.ParameterType,
							valueText );
					}
				}
			}

			// Format the name-value pair.

			string FormattedText = String.Empty;
			
			if( DisplayParameter )
			{
				FormattedText = FormatNameValuePair( name, FormattedValue );
			}
			
			return FormattedText;
		}
		
		/// <summary>
		/// Gets the web service parameter at the specified index.
		/// </summary>
		/// <param name="parameters">A collection of parameters.</param>
		/// <param name="index">The parameter index.</param>
		/// <returns>The web service parameter if the index is valid otherwise
		/// returns null.</returns>
		private static WebServiceParameter GetParameter( WebServiceParameterCollection parameters,
			int index )
		{
			WebServiceParameter Parameter = null;

			if( parameters.Count > index )
			{
				Parameter = parameters[ index ];
			}
			return Parameter;
		}

		/// <summary>
		/// Formats a name value pair.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="valueText">The value.</param>
		/// <returns>A formatted name value pair.</returns>
		private static string FormatNameValuePair( string name, string valueText )
		{
			return String.Concat( name, ": ", 
				valueText, "\r\n" );
		}
	}
}
